/*
 * LavaSlider v1.0.0
 * A super light-weight jQuery image slider
 * http://lavasnake.com/lavaslider/
 * (c) 2010 - Aaron Benson - bensonan55 [ at ] gmail [ dot ] com
 * MIT License - http://www.opensource.org/licenses/mit-license.php
 */
// Configuration
var useTimer = true;
var useCircles = true;
var slideWidth = 500;
var timerDuration = 4000;
// Init vars
var xPos = 0;
var totalWidth;
var left = "left";
var right = "right";
var interval;
var totalSlides;
var currentSlide = 0;
// Document on ready
$( document ).ready( function()
{
	// Define vars
	totalSlides = $( '.slider_image' ).length;
	totalWidth = slideWidth * totalSlides;
	// Apply proterties
	$( '#image_container' ).width( totalWidth );
	// Add pagination
	if ( useCircles == true ) createSliderLinks();
	// Start timer
	resetTimer();
	// Next button event listener
	$( '#slider_move_next' ).click( function()
	{
		resetTimer();
		slide( updateX( right ) );
	});
	// Previous button event listener
	$( '#slider_move_prev' ).click( function()
	{
		resetTimer();
		slide( updateX( left ) );
	});
});
// Create navigation
function createSliderLinks()
{
	for ( var i = 0; i < totalSlides; i++ )
	{
		$( '.slider_circles' ).append( '<a href="#" id="slide_nav_item_'+i+'">'+i+'</a>' );
	}
	// Add event listeners to slide nav
	$( '.slider_circles a' ).bind( 'click', function() { onNavClick( this ); } );
	updateNavClass( 0 );
}
// Reset timer
function resetTimer()
{
	if ( useTimer == true )
	{
		if ( interval != null )
		{
			window.clearInterval( interval );
		}
		interval = window.setInterval( "onInterval();", timerDuration );
	}
}
// Animate slides
function slide( pos )
{
	$( '#image_container' ).stop( true, false );
	$( '#image_container' ).animate( { left: pos }, 1000 );
}
// On nav click
function onNavClick( element )
{
	resetTimer();
	var id = element.id;
	var index = id.lastIndexOf( "_" + 1, 0 );
	var num = parseInt( id.substr( index, id.length ) );
	slide( num * -slideWidth );
	updateNavClass( num );
}
//
function updateNavClass( id )
{
	$( '.slider_circles a').removeClass( 'active' );
	$( '#slide_nav_item_' + id ).addClass( 'active' );
}
// Update, return new x position
function updateX( dir )
{
	switch ( dir )
	{
		case left :
			if ( xPos < 0 )
			{
				xPos += slideWidth;
				currentSlide--;
			} else {
				xPos = -totalWidth + slideWidth;
				currentSlide = totalSlides - 1;
			}
			break;
		case right :
			if ( xPos > -totalWidth + slideWidth )
			{
				xPos -= slideWidth;
				currentSlide++;
			} else {
				xPos = 0;
				currentSlide = 0;
			}
			break;
		default :
			break;
	}
	updateNavClass( currentSlide );
	return xPos;
}
// Timer event handler
function onInterval()
{
	slide( updateX( right ) );
}